<template>
  <div class="table">
    <div class="handle-box">
      <el-input v-model="selectWord" size="mini" placeholder="请输入资源类型名" class="handle-input" ></el-input>
      <el-button type="primary" size="mini" @click="centerDialogVisible=true">添加标签</el-button>
      <el-button type="primary" size="mini" @click="batchDelete">批量删除</el-button>
    </div>
    <el-table size="mini" border style="width: 100%" height="580px" :data="data" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40"></el-table-column>

      <el-table-column prop="name" label="资源类型名" width="120" align="center">
      </el-table-column>
      <el-table-column prop="sex" label="操作"  width="180" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini"  type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @current-change="handleCurrentChange"
        layout = "total,prev,pager,next"
      >
      </el-pagination>
    </div>
    <el-dialog title="添加资源类型" :visible.sync="centerDialogVisible" width="400px" center>
      <el-form :model="registerForm" ref="registerForm" label-width="80px">
        <el-form-item prop="name" label="资源类型名" size="mini">
          <el-input v-model="registerForm.name" placeholder="资源类型名"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
          <el-button size="mini" @click="centerDialogVisible=false">取消</el-button>
          <el-button size="mini" @click="addStudySourceStyle">确定</el-button>
        </span>
    </el-dialog>
    <el-dialog title="修改资源类型" :visible.sync="EditVisible" width="400px" center>
      <el-form :model="editForm" ref="editForm" label-width="80px">
        <el-form-item prop="name" label="资源类型名" size="mini">
          <el-input v-model="editForm.name" placeholder="资源类型名"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
          <el-button size="mini" @click="EditVisible=false">取消</el-button>
          <el-button size="mini" @click="updateTag">确定</el-button>
        </span>
    </el-dialog>
    <el-dialog title="删除资源类型" :visible.sync="delVisible" width="300px" center>
      <div align="center">删除不可恢复，是否确定删除？</div>
      <el-form :model="delForm" ref="editForm" label-width="80px">
      </el-form>
      <span slot="footer">
                <el-button size="mini" @click="delVisible = false">取消</el-button>
                <el-button size="mini" @click="deleteRow">确定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "VideoStyleManage",
    data(){
      return {
        centerDialogVisible:false,
        EditVisible:false,
        delVisible:false,
        delForm:{
          id:'',
        },
        editForm:{
          id:'',
          name:'',
        },
        registerForm:{
          name:'',
        },
        tableData:[],
        templateData:[],
        selectWord:'',
        currentPage:1,
        pageSize:10,
        selectIds:[],
      }
    },
    computed:{
      data(){
        return this.tableData.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize);
      }
    },
    watch:{
      selectWord:function () {
        if(this.selectWord==''){
          this.tableData=this.templateData;
        }else{
          this.tableData=[];
          for(let item of this.templateData){
            if(item.name.includes(this.selectWord)){
              this.tableData.push(item);
            }
          }
        }
      }
    },
    created() {
      this.getAllStudySourceStyles();
    },
    methods:{
      addStudySourceStyle(){
        var _this=this;
        this.$axios.post("http://localhost:8888/StudySourceStyle/insertStudySourceStyle",this.registerForm).then(function (data) {
          if(data.status==200){
            _this.$notify({
              title: '新增成功',
              type: 'success'
            });
            _this.getAllStudySourceStyles();
          }else{
            _this.$notify({
              title: '新增失败',
              type: 'error'
            });
          }
          _this.centerDialogVisible=false;
        })
      },
      getAllStudySourceStyles(){
        var _this=this;
        this.$axios.get("http://localhost:8888/StudySourceStyle/selectAllStudySourceStyle").then(function (data) {
          console.log(data);
          _this.tableData=data.data;
          _this.templateData=data.data;
        })
      },

      handleEdit(row){
        this.EditVisible=true;
        this.editForm  = {
          id:row.id,
          name:row.name,
        }
      },
      handleDelete(row){
        this.delVisible=true;
        this.delForm = {
          id:row.id,
        }
      },
      deleteRow(){
        var _this=this;
        var id=this.delForm.id;
        this.$axios.delete("http://localhost:8888/StudySourceStyle/deleteStudySourceStyle/"+id).then(function (data) {
          if(data.status==200){
            _this.$notify({
              title: '删除成功',
              type: 'success'
            });
            _this.getAllStudySourceStyles();
          }else{
            _this.$notify({
              title: '删除失败',
              type: 'error'
            });
          }
          _this.delVisible=false;
        })
      },
      updateTag(){
        var _this=this;
        this.$axios.post("http://localhost:8888/StudySourceStyle/updateStudySourceStyle",this.editForm).then(function (data) {
          if(data.status==200){
            _this.$notify({
              title: '修改成功',
              type: 'success'
            });
            _this.getAllStudySourceStyles();
          }else{
            _this.$notify({
              title: '修改失败',
              type: 'error'
            });
          }
          _this.EditVisible=false;
        })
      },
      handleSelectionChange(val){
        this.selectIds=val;
      },
      batchDelete(){
        var _this=this
        var ids=''
        for(let item of this.selectIds){
          ids=ids+item.id+',';
        }
        this.$axios.get("http://localhost:8888/singer/deleteSingers/"+ids).then(function (data) {
          if(data.status==200){
            _this.$notify({
              title: '删除成功',
              type: 'success'
            });
            _this.getAllStudySourceStyles();
          }else{
            _this.$notify({
              title: '删除失败',
              type: 'error'
            });
          }
        })
      },
      handleCurrentChange(val){
        this.currentPage=val;
      }
    }
  }
</script>

<style scoped>
  .handle-box{
    margin-bottom: 20px;
  }

  .singer-img{
    width: 100%;
    /* height: 40px;*/
    border-radius: 5px;
    border-bottom: 5px;
    overflow: hidden;
  }
  .handle-input{
    width: 300px;
  }
  .pagination{
    display: flex;
    justify-content: center;
  }
</style>
